<?php
declare (strict_types = 1);

namespace app\command;

use app\model\BaseModel;
use app\model\Promotion;
use app\model\ReportUser;
use app\util\CacheHelper;

class UserReportCommand extends BaseCommand
{
    protected $scriptName = "userReport";

    protected function _execute()
    {
        $redisKey = config('cachekeys.user_report');
        $redis = new CacheHelper();
        $len = $redis->llen($redisKey);
        if (empty($len)) {
            echo 'nodataa';
            return false;
        }

        $promotion = new Promotion();
        $reportUser = new ReportUser();
        $reportJHKey = config('cachekeys.user_report_jh_num');
        $reportCZKey = config('cachekeys.user_report_cz_num');
        $reportTgKey = config('cachekeys.user_report_tg');
        while ($res = $redis->rpop($redisKey)) {
            echo 'start:' . PHP_EOL;
            var_dump($res);
            if (empty($res)) {
                echo 'nores' . PHP_EOL;
                continue;
            }
            $data = json_decode($res, true);
            if (empty($data)) {
                echo 'nojsondata' . PHP_EOL;
                continue;
            }
            $trackCode = $data['trackCode']; // 跟踪号
            $goodsType = $data['goodsType']; // 商品类型
            $orderTotalNum = $data['orderTotalNum']; // 订单总数
            $yjMoney = $data['yjMoney']; // 佣金金额

            echo 'hhh' . PHP_EOL;
            $promotion = $promotion->where(['trackingCode' => $trackCode])->find();
            if (empty($promotion)) {
                echo 'nopromotion' . PHP_EOL;
                continue;
            }
            $pUserId = $promotion['parent_user_id']; // 上级userid
            $user_id = $promotion['user_id']; // 推广人id
            $subordinate_name = $promotion['subordinate_name']; // 下线名字
            $phone = $promotion['subordinate_account'];
            $reportJHKey = $reportJHKey . $user_id;
            $reportCZKey = $reportCZKey . $user_id;
            $jhNum = $redis->scard($reportJHKey); //  激活数量
            $czNum = $redis->scard($reportCZKey); // 充值数量
            $reportTgKey = $reportTgKey . $user_id;
            $tgGoodsNum = $redis->scard($reportTgKey); //  推广商品数量
            echo 'jhKey:' . $reportJHKey . ":v:" . $jhNum . PHP_EOL;
            echo 'czKey:' . $reportCZKey . ":v:" . $czNum . PHP_EOL;
            echo 'tgKey:' . $reportTgKey . ":v:" . $tgGoodsNum . PHP_EOL;
            BaseModel::beginTrans();
            echo 'swstart:' . PHP_EOL;
            try {
                // 推广表更新
                $promotion->jh_num = $jhNum;
                $promotion->cz_num = $czNum;
                $promotion->total_money += $yjMoney;
                $promotion->order_num += $orderTotalNum;
                $promotion->save();

                $reportUserData = $reportUser->where(['fx_user_id' => $user_id])->find();
                if (empty($reportUserData)) {
                    // 没有则新增
                    $addData['fx_user_id'] = $user_id;
                    $addData['fx_user_name'] = $subordinate_name;
                    $addData['phone'] = $phone;
                    $addData['goods_num'] = 1;
                    $addData['order_num_total'] = $orderTotalNum;
                    $addData['yj_money'] = $yjMoney;
                    $addData['puser_id'] = $pUserId;
                    switch ($goodsType) {
                        // 商品类型0-号卡 1-流量包 2-靓号 3-宽带
                        case 1:
                            $addData['lb_order_num'] = 1;
                            break;
                        case 2:
                            $addData['lh_order_num'] = 1;
                            break;
                        case 3:
                            $addData['kd_order_num'] = 1;
                            break;
                        case 0:
                            $addData['hk_order_num'] = 1;
                            break;
                    }
                    echo 'add' . PHP_EOL;
                    $reportUser->save($addData);
                } else {
                    $reportUserData->goods_num = $tgGoodsNum;
                    $reportUserData->order_num_total += $orderTotalNum;
                    switch ($goodsType) {
                        // 商品类型0-号卡 1-流量包 2-靓号 3-宽带
                        case 1:
                            $reportUserData->llb_order_num += 1;
                            break;
                        case 2:
                            $reportUserData->lh_order_num += 1;
                            break;
                        case 3:
                            $reportUserData->kd_order_num += 1;
                            break;
                        case 0:
                            $reportUserData->hk_order_num += 1;
                            break;
                    }
                    $reportUserData->yj_money += $yjMoney;
                    $reportUserData->save();
                }

                BaseModel::commitTrans();
                echo 'swok' . PHP_EOL;
            } catch (\Exception $e) {
                BaseModel::rollbackTrans();
                echo 'swerr';
                if (!empty($res)) {
                    $redis->lpush($redisKey, $res);
                }
                echo $e->getMessage() . PHP_EOL;
            }
        }
    }
}
